/**
 * @author tmh
 * @date 2024/9/25 21:04
 * @description 这里需要用小于等于的写法，然后最后得到的right就是小于target的第一个数的下标
 */
public class T35搜索插入位置 {
    public int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            int mid = (right + left) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid-1;
            } else {
                return mid;
            }
        }
        return right+1;
    }
}
